package dymnd.ui;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class LoadingPane extends JComponent implements Runnable, MouseListener{
	
	String text = null;
	int barsCount = 15;
	int shield = 50;
	int fps = 10;
	long rampDelay = 0;
	Area [] ticker;
	boolean started = false;
	boolean rampUp = false;
	int alphaLevel = 70;
	
	public LoadingPane()	{
			
	}
	public void start()	{
		ticker = buildTicker();
		setVisible(true);
		started = true;
		new Thread(this).start();
	}
	
	private Area buildPrimitive()	{
		Rectangle2D.Double body = new Rectangle2D.Double(6, 0, 30, 12);
		Ellipse2D.Double head = new Ellipse2D.Double(0,0,12,12);
		Ellipse2D.Double tail = new Ellipse2D.Double(30,0,12,12);
		
		Area tick = new Area(body);
		tick.add(new Area(head));
		tick.add(new Area(tail));
		
		return tick;
	}
	
	public void run()	{
		started = true;
		Point2D.Double center;
		center = new Point2D.Double((double)getWidth() /2, (double) getHeight()/2);
		double fixedIncrement = 2.0 * Math.PI / ((double)barsCount);
		System.out.println("Started!");
		AffineTransform toCircle = AffineTransform.getRotateInstance(fixedIncrement, center.getX(), center.getY());
		long start = System.currentTimeMillis();
		if(rampDelay == 0)	{
			alphaLevel = rampUp ? 255 : 0;
		}
		started = true;
		boolean inRamp = rampUp;
		
		while(!Thread.interrupted())	{
			if(!inRamp)	{
				for(int i = 0; i < ticker.length; i++)
					ticker[i].transform(toCircle);
			}
			repaint();
			
			try	{
				Thread.sleep(inRamp ? 10 : (int) (1000 / fps));
			}	catch(InterruptedException x)	{
				break;
			}
			//Thread.yield();
		}
		
		
	}
	
	public void paintComponent(Graphics arg0)	{
		if(started && ticker != null)	{
			Graphics2D g = (Graphics2D) arg0;
			int width = getWidth();
			int height = getHeight();
			
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			
			g.setColor(new Color(255, 255, 255, shield));
			g.fillRect(0, 0, width, height);
			
			for(int i = 0; i < ticker.length; i++)	{
				int channel = 224 - 128 / (i+1);
				g.setColor(new Color(channel, channel, channel, alphaLevel));
				g.fill(ticker[i]);
			}
			
		}
	}
	
	
	private Area[] buildTicker()	{
		Area[] ticker = new Area[barsCount];
		Point2D.Double center = new Point2D.Double((double)getWidth()/2, (double)getHeight()/2);
		
		double fixedAngle = 2.0 *Math.PI / ((double)barsCount);
		
		for(double i = 0.0; i < (double)barsCount; i++)	{
			//AHHHHHH, scary scary math!!! D= 
			Area primitive = buildPrimitive();
			AffineTransform toCenter = AffineTransform.getTranslateInstance(center.getX(), center.getY());
			AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0, -6.0);
			AffineTransform toCircle = AffineTransform.getRotateInstance(-i * fixedAngle,center.getX(), center.getY());
			AffineTransform toWheel = new AffineTransform();
			toWheel.concatenate(toCenter);
			toWheel.concatenate(toBorder);
			
			primitive.transform(toWheel);
			primitive.transform(toCircle);
			
			ticker[(int)i] = primitive;
		}
		return ticker;
	}
	
	public void mouseReleased(MouseEvent e)	{}
	public void mousePressed(MouseEvent e)	{}
	public void mouseClicked(MouseEvent e)	{}
	public void mouseEntered(MouseEvent e)	{}
	public void mouseExited(MouseEvent e)	{}
}